﻿using System;
using System.Collections.Generic;
using System.Web;
using EPiServer.Web.Hosting;
using System.Web.Hosting;
using EPiServer.Web;

namespace EPiServer.Labs.MobilePack.Site.modules.MobilePack
{
    public partial class ImageGallery : MobileBase
    {
        protected override void OnLoad(EventArgs e)
        {
            BindFilesFromFolder();
            
            base.OnLoad(e);
        }

        public void BindFilesFromFolder()
        {
            try
            {
                bool loadFromPageFiles = true;

                string path = CurrentPage["GalleryFolder"] as string;
                if (path != null)
                {
                    UnifiedDirectory directory = HostingEnvironment.VirtualPathProvider.GetDirectory(path) as UnifiedDirectory;
                    if (directory != null)
                    {
                        List<UnifiedFile> files = new List<UnifiedFile>(directory.GetFiles());
                        if (files.Count > 0)
                        {
                            filesRepeater.DataSource = files;
                            filesRepeater.DataBind();
                            loadFromPageFiles = false;
                        }
                    }
                }

                if (loadFromPageFiles)
                {
                    LoadFromPageFiles();
                }
            }
            catch
            {
                EditorMessage.Text = "The Image Gallery could not be loaded, please check the path";
                ImageGalleryPlaceHolder.Visible = false;
            }
        }

        private void LoadFromPageFiles()
        {
            int folderId = (int) CurrentPage.Property["PageFolderID"].Value;

            string pageDirectoryRootVirtualPath = VirtualPathHandler.PageDirectoryRootVirtualPath;
            string virtualPathFromFolderId = VirtualPathUtilityEx.Combine(pageDirectoryRootVirtualPath, VirtualPathUtility.AppendTrailingSlash(folderId.ToString()));

            UnifiedDirectory directory =
                HostingEnvironment.VirtualPathProvider.GetDirectory(virtualPathFromFolderId) as UnifiedDirectory;
            if (directory != null)
            {
                List<UnifiedFile> files = new List<UnifiedFile>(directory.GetFiles());

                if (files.Count == 0)
                {
                    MessageForEditors();
                }

                filesRepeater.DataSource = files;
                filesRepeater.DataBind();
            }
            else
            {
                MessageForEditors();
            }
        }

        private void MessageForEditors()
        {
            if (Security.PrincipalInfo.HasEditAccess)
            {
                EditorMessage.Text = "This image gallery is empty. Add images to this page's PageFolder to activate it.";
                ImageGalleryPlaceHolder.Visible = false;
            }
        }
    }
}
